const app = getApp()
const middleware = require('../../utils/middleware.js')
import regeneratorRuntime from '../../lib/wxPromise.min.js'
import * as API_Trade from '../../api/trade'
import * as API_Order from '../../api/order'
import * as API_Member from '../../api/members'
import {
  Foundation
} from '../../ui-utils/index'
import {
  md5
} from '../../lib/md5'

Page(middleware.identityFilter({
  data: {
    trade_sn: '',
    order_sn: '',
    // 支付方式
    payment: '',
    // 订单详情
    order: '',
    //订单状态
    order_status: '',
    // 显示确认订单完成支付dialog
    //showConfirmDialog: false,
    /* 余额支付 */
    password: '',
    balance_status: true,
    show_balance: false,
    focus: false,
    isRecharge: false
  },
  onLoad(options) {
    this.setData({
      order_sn: options.order_sn || '',
      trade_sn: options.trade_sn || '',
      isRecharge: options.isRecharge || false
    })
  },
  async onShow() {
    const responses = await Promise.all([
      this.data.trade_sn ?
      API_Trade.getCashierData({
        trade_sn: this.data.trade_sn
      }) :
      API_Trade.getCashierData({
        order_sn: this.data.order_sn
      }),
      API_Trade.getPaymentList('WAP')
    ])
    console.log('responses')
    console.log(responses)
    this.setData({
      order: {
        ...responses[0],
        need_pay_price: Foundation.formatPrice(responses[0].need_pay_price)
      },
      payment: responses[1][1],
      show_balance: false,
      focus: false
    })
  },
  choosePassword(e) {
    this.setData({
      password: e.detail.value
    }, () => {
      console.log(this.data.password);
    })
  },
  /** 发起支付 */
  initiatePay() {
    const trade_type = this.data.trade_sn ? 'trade' : 'order'
    const sn = this.data.trade_sn || this.data.order_sn
    const client_type = 'MINI'
    const payment_plugin_id = this.data.payment.plugin_id
    API_Trade.initiatePay(trade_type, sn, {
      client_type,
      pay_mode: 'normal',
      payment_plugin_id
    }).then(response => {
      const params = {}
      response.form_items.forEach(item => {
        params[item.item_name] = item.item_value
      })
      wx.pro.requestPayment(params).then(res => {
        wx.showModal({
          title: '支付提示',
          content: '请确认支付是否完成',
          confirmText: '支付成功',
          cancelText: '重新支付',
          success(res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 3
              })
            } else if (res.cancel) { // 用户点击取消
            }
          }
        })
      })
    })
  },
  /* 余额支付 */
  balancePay() {
    API_Member.seeApyPassword().then(res => {
      this.setData({
        balance_status: res,
        show_balance: true
      })
      if (res) {
        this.setData({
          focus: true
        })
      }
    })
  },
  /* 关闭弹窗 */
  close() {
    this.setData({
      show_balance: false,
      focus: false
    })
  },
  /* 设置密码 */
  paySetting() {
    wx.navigateTo({
      url: "/ucenter/setting/balance-password/balance-password?type=cashier",
      // url:"/ucenter/accountSafe/balance-password/balance-password?type=cashier"
    })
  },

  /* 确定支付 */
  payBalance() {
    let password = this.data.password
    this.setData({
      show_balance: false,
      focus: false
    });
    const trade_type = this.data.trade_sn ? 'trade' : 'order'
    const sn = this.data.trade_sn || this.data.order_sn
    API_Trade.balancePay(trade_type, sn, {
      password: md5(password)
    }).then(res => {
      wx.showToast({
        title: '支付成功!'
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 3
        })
      }, 800)
    })
  },
  // 查看订单
  handleLookOrder() {
    wx.navigateTo({
      url: '/ucenter/order/order'
    })
  },
  // 回到首页
  handleToHome() {
    wx.switchTab({
      url: '/pages/home/home'
    })
  }
}))